/*
 * @Author: wuzhen
 * @Date: 2020-01-15 14:52:34
 * @LastEditors: wuzhen
 * @LastEditTime: 2020-04-18 13:58:54
 * @Description 聊天
 */
import { formatLetter } from "@/libs/units";
import {
  getFriendShips,
  getGroupList,
  getCountOfflineMessage,
  userLogout
} from "@/request/api";
const chat = {
  state: {
    jsessionId: null, // jsessionId
    unf: null, // unf
    userInfo: {}, // 当前用户信息
    onlineUserIds: [], // 在线用户id
    friends: [], // 好友
    groups: [], // 群组
    chatUsers: [], // 正在聊天用户
    chatMsg: [], // 所有聊天信息
    chatting: {}, // 正在聊天人信息
    input_httpSeqno: null // 正在输入时间戳
  },
  mutations: {
    set_jsessionId: (state, data) => {
      state.jsessionId = data;
    },
    set_unf: (state, data) => {
      state.unf = data;
    },
    set_friends: (state, data) => {
      state.friends = data;
    },
    set_groups: (state, data) => {
      state.groups = data;
    },
    set_chatUsers: (state, data) => {
      state.chatUsers = data;
    },
    set_chatMsg: (state, data) => {
      state.chatMsg = data;
    },
    set_chatting: (state, data) => {
      state.chatting = data;
    },
    set_onlineUserIds: (state, data) => {
      state.onlineUserIds = data;
    },
    set_userInfo: (state, data) => {
      state.userInfo = data;
    },
    set_input_httpSeqno: (state, data) => {
      state.input_httpSeqno = data;
    }
  },
  actions: {
    // 获取好友数据
    async getFriendData({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await getFriendShips();
          const data = res.result.rows.map(v => {
            const name = v.friendname || v.friend.username;
            return {
              ...{
                name: name,
                username: v.friend.username,
                id: v.friendid,
                face: v.friend.face || "",
                letter: formatLetter(name),
                msgkind: 20,
                mobile: v.friend.mobile,
                personlsign: v.friend.personlsign || "",
                secretstatus: v.secretstatus,
                secret: v.secret || ""
              }
            };
          });
          commit("set_friends", data);
          resolve(data);
        } catch (error) {
          reject(error);
        }
      });
    },
    // 获取群组数据
    async getGroupData({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await getGroupList();
          const data = res.result.map(v => {
            return {
              ...{
                grouptype: v.grouptype,
                usernum: v.usernum,
                name: v.groupname,
                id: v.groupid,
                face: v.groupface,
                letter: "群聊",
                msgkind: 30,
                createuserid: v.createuserid,
                syssetting: v.syssetting // 1是开启禁言，0是关闭禁言
              }
            };
          });
          commit("set_groups", data);
          resolve(res);
        } catch (error) {
          reject(error);
        }
      });
    },
    // 获取聊天成员消息
    async getChatUsers({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await getCountOfflineMessage();
          commit("set_chatUsers", res.result);
          resolve(res);
        } catch (error) {
          reject(error);
        }
      });
    },
    // 退出登录
    async onLogout({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await userLogout();
          commit("set_jsessionId", null);
          commit("set_unf", null);
          commit("set_friends", []);
          commit("set_groups", []);
          commit("set_chatUsers", []);
          commit("set_chatMsg", []);
          commit("set_chatting", {});
          commit("set_onlineUserIds", []);
          commit("set_userInfo", {});
          resolve(res);
        } catch (error) {
          reject(error);
        }
      });
    },
    // 清除缓存
    async onClearStroage({ commit }) {
      commit("set_jsessionId", null);
      commit("set_unf", null);
      commit("set_friends", []);
      commit("set_groups", []);
      commit("set_chatUsers", []);
      commit("set_chatMsg", []);
      commit("set_chatting", {});
      commit("set_onlineUserIds", []);
      commit("set_userInfo", {});
    }
  }
};

export default chat;
